<?php
class SettingController extends Controller{
	public $tabView;
	public $layout = 'setting';
	public $user;
	public function actionIndex(){
		if(Yii::app()->user->isGuest)Yii::app()->user->loginRequired();
		$this->user = User::model()->findByPk(Yii::app()->user->id);
		//Yii::app()->clientScript->registerScriptFile("/js/jquery.Jcrop.min.js");
		if(Yii::app()->user->logintype == 'self'){
			$this->render('index',array('user'=>$this->user));
		}else{
			$this->layout = 'layout1';
			$this->render('index_user',array('user'=>$this->user));
		}
	}
	public function actionUpload(){
		$uid = NC::_g('id',0);
		if($data = file_get_contents("php://input","r")){
			$path = NC::get_icon($uid);
		 	$uploadfile = 'uploads/'.$_SERVER['REQUEST_TIME'].'.jpg';
		 	$path = str_replace('\\','/',$path);
		 	//echo $path;exit;
		 	
			if(!file_put_contents($path,$data)){
				echo '{"info":"上传头像失败！"}';exit;
			}else{
				echo '{"info":"上传头像成功！"}';
			}
		}else{
				$img = new PictureUpload('icon',null,'Filedata'); 
				$photo = $img->upLoad();
				$photo = $img->smallImg($photo,450,450,$uid);
				$photo = '/' . str_replace('\\', '/', $photo);
				echo json_encode(array('path'=>$photo,'post'=>$_POST));
		}
		
	}
	public function actionFavor(){
		if(Yii::app()->user->isGuest)Yii::app()->user->loginRequired();
		$this->user = User::model()->findByPk(Yii::app()->user->id);
		$this->render('favor',array('user'=>$this->user));
	}
	public function actionBase(){
		if(Yii::app()->request->isAjaxRequest){
			$profileAR = new Profiles('setting');
			$profile = $profileAR->findByPk(Yii::app()->user->id);
			$post = $_POST;
			foreach($post as $key => $val)$post[$key] = urldecode($val);
			$profile->attributes = $post;
			$birthday = $post['birthday'];
			if($birthday){
				$birth = @explode('-',$birthday);
				$profile->birth_year = $birth[0];
				$profile->birth_month = $birth[1];
				$profile->birth_day = $birth[2];
			}
			$profile->gender = $this->toGender($post['gender']);
			$profile->user_id = Yii::app()->user->id;
			if(!$profile->save())
				NC::json(false,$profile->getErrors());
			NC::json(true);
		}else{
			Yii::app()->clientScript->registerScriptFile("/js/nc.canlendar.js");
			Yii::app()->clientScript->registerScriptFile("/js/nc.area.js");
			if(Yii::app()->user->isGuest)Yii::app()->user->loginRequired();
			$this->user = User::model()->findByPk(Yii::app()->user->id);
			$this->render('base',array('user'=>$this->user));
		}
	}
	public function toGender($gender){
		$dataformate = array('男'=>'man','女'=>'women');
		return isset($dataformate[$gender]) ? $dataformate[$gender] : '';
	}
	public function actionPassword(){
		$account = new User('setting');
		if(isset($_POST['ajax']) && $_POST['ajax'] == 'setting-form'){
			echo CActiveForm::validate($account);
			Yii::app()->end();
		}
		if(Yii::app()->request->isPostRequest){$post = array();
			foreach($_POST as $key => $value){
				$tmpKey = urldecode($key);
				//if(strpos($tmpKey,'email'))$account->email = urldecode($value);
				if(strpos($tmpKey,'oldPassword'))$account->oldPassword = urldecode($value);
				if(strpos($tmpKey,'newPassword'))$account->newPassword = urldecode($value);
				if(strpos($tmpKey,'cPassword'))$account->cPassword = urldecode($value);
				if(strpos($tmpKey,'password'))$account->password = urldecode($value);
			}
			if($account->oldPassword == $account->newPassword){NC::json(false,'您的密码没有修改！');}
			if($account->validate()){
				if(User::model()->updateByPk(Yii::app()->user->id,array('password'=>md5($account->newPassword)))){
					$account->oldPassword = $account->newPassword;
					NC::json(true);
				}
			}
			NC::json(false,$account->getErrors());
		}
		if(Yii::app()->user->isGuest)Yii::app()->user->loginRequired();
		$this->user = User::model()->findByPk(Yii::app()->user->id);
		$this->render('password',array('user'=>$this->user,'account'=>$account));
	}
	//使用PHP裁剪，已经过时
	public function actionIcon(){
		$targ_w = $targ_h = 100;
	$jpeg_quality = 100; 
	
	$host = NC::get_header();
	$src =$host . DS . NC::get_icon(Yii::app()->user->id).'_thumb';
	//$src = rename($src,$src.'.jpg');
	$src = str_replace('\\', '/', $src);
	$img = imagecreatefromjpeg($src);//echo 'dfd';exit;
	list($width, $height, $type, $attr) = getimagesize($src);
	//$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
	//$whites = imagecolorallocate($dst_r,255,255,255);  //背景白色
	//imagefill($dst_r,0,0,$whites);//dbg($whites);
	if (function_exists("imagecreatetruecolor")) {
		$newImg = imagecreatetruecolor($targ_w, $targ_h);
		imagecopyresampled($newImg, $img, 0, 0, $_POST['x'],$_POST['y'], $targ_w, $targ_h,$_POST['w'],$_POST['h']);
	} else {
			$newImg = imagecreate($width, $height);
			imagecopyresized($newImg, $img, 0, 0, $_POST['x'],$_POST['y'],  $targ_w, $targ_h,$_POST['w'], $_POST['h']);
	}
	//header('Content-type: image/jpeg');
	//imagejpeg($newImg, '', $jpeg_quality);
	ob_start();
	$rr = imagejpeg( $newImg, "", $jpeg_quality );
    $i = ob_get_contents();
	ob_end_clean();
    // Save file
	$uid = Yii::app()->user->id;
	
	$destImage = NC::get_icon($uid);//echo $destImage;exit;
	$path =  dirname(__FILE__);
	$path = $path.DS.'..'.DS. '..' . DS . 'public'.DS.$destImage;//echo $destImage;exit;
	$path = str_replace('\\', '/', $path);
	if(file_exists($path)){
	//dbg($path);
		//$r = unlink($path);
		//if($r){
			$fp = fopen($path,'w');
			fwrite ($fp, $i);
			fclose ($fp);
			if(file_exists($path.'_thumb')){
				$r = unlink($path.'_thumb');
			}
			$this->redirect('/upper');
			exit;	
		
	}else{
		//echo "<script>info(\"'图片不存在'\",location.href='/user/iconset/id/".$_SESSION['uid']."');</script>";
		$fp = fopen($path,'w');
		fwrite ($fp, $i);
		fclose ($fp);
	} 
	$this->redirect('/upper');
	exit;
	}
}